﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseCheckMaxFileGrowthStateOn : IssueTypeCheckBase
    {

        public DatabaseCheckMaxFileGrowthStateOn()
        {
            IssueTypeId = 3009;
        }

        public override void Check()
        {
            try
            {
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


                /*
                   * This rule checks if there is a specified Max Files Size limit if value is -1 growth is said to be Unrestricted..
                   */

                var QryMaxGrowth = from d in dc.DatabaseFiles

                          where d.MaxSize != -1 && d.MaxSize < Convert.ToInt32(d.SizeInMb) * 256
                          select d;

                foreach (var database in QryMaxGrowth)
                {
                    IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                    //Create new Issue

                    Issue issue = new Issue();
                    issue.IssueType = issueType;
                    issue.TargetId = database.DatabaseID;
                    issue.Description = issueType.Description.Replace("{Database}", database.Database.DatabaseName).Replace("{MaxSize}", database.MaxSize.ToString()).Replace("{File}", database.File.ToString());






                    dc.Issues.InsertOnSubmit(issue);


                }

                dc.SubmitChanges();


            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

    }
}
